package com.zfast.yugioh.junit;

import cn.hutool.core.lang.WeightRandom;
import cn.hutool.core.lang.WeightRandom.WeightObj;
import cn.hutool.core.util.*;
import com.zfast.yugioh.bo.EffectBo;
import com.zfast.yugioh.bo.GameBo;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author zgn
 * @Description: simJunit
 * @date 2022/1/13 0013
 */
public class huToolJunit {
    @Test
    public void egMax() {//e.g.
        //region 1.模拟数据
        System.out.println("--------------------Start--------------------");
        //endregion

        //region 2.数据处理
        System.out.println("--------------------Deal--------------------");
        //endregion

        //region 3.数据展示
        System.out.println("--------------------Done--------------------");
        //endregion
    }
    @Test
    public void 权重随机数_egMin() {//e.g.
        List<WeightObj<String>> weightList = new ArrayList<WeightObj<String>>();
        WeightObj<String> a = new WeightObj<String>("A", 1);
        WeightObj<String> b = new WeightObj<String>("B", 2);
        WeightObj<String> c = new WeightObj<String>("C", 3);
        WeightObj<String> d = new WeightObj<String>("D", 4);
        weightList.add(a);
        weightList.add(b);
        weightList.add(c);
        weightList.add(d);

        WeightObj<String>[] weightArry= new WeightObj[]{a, b, c, d};
        WeightRandom wr = RandomUtil.weightRandom(weightArry);
        String str = "";
        int num_a = 0, num_b = 0, num_c = 0, num_d = 0;
        for (int i = 0; i < 100000; i++) {
            str = wr.next().toString();
            switch (str) {
                case "A":
                    num_a = num_a+1;
                    break;
                case "B":
                    num_b = num_b+1;
                    break;
                case "C":
                    num_c = num_c+1;
                    break;
                case "D":
                    num_d = num_d+1;
                    break;
            }
            //System.out.println(str);
        }
        System.out.println("A---------"+num_a);
        System.out.println("B---------"+num_b);
        System.out.println("C---------"+num_c);
        System.out.println("D---------"+num_d);
    }
    @Test
    public void 空字串判断_egMin() {//e.g.
        //false:"",null
        //reue:"null"
        String s="null";
        System.out.println(StrUtil.isNotBlank(s));
    }

    @Test
    public void 数组最小值_egMin() {//e.g.indexOf
        Integer []firstNum= {4,2,3,5,8};
        Integer min = ArrayUtil.min(firstNum);
        System.out.println(min);
    }

    @Test
    public void 数组对象位置_egMin() {//e.g.
        String[] keywords={"select ","update "," set "," from "," to "," where "," limit "};

        String[] KeyValue=new String[keywords.length];
        for (int i = 0; i < keywords.length; i++) {
            KeyValue[i] = keywords[i].replace(" ","");

        }
        System.out.println(ArrayUtil.indexOf(KeyValue,"select"));
    }
    @Test
    public void 获取字串括号内内容_egMin() {//e.g.
        String effctStr="update Card set race=? from GameDetailBoMe.cards to GameDetailBoMe.handCards " +
                "where ((type=?) or race=?) and DestroyedReason " +
                "limit 0,1";
        String str = "username(zhangsan)";
        String regex = "(\\(([^()]|(?R))*\\))";
        String group0 = ReUtil.getGroup0(regex, effctStr);
        String group1 = ReUtil.getGroup1(regex, effctStr);
        System.out.println("group0 = " + group0);
        System.out.println("group1 = " + group1);
    }


    @Test
    public void 反映射_获取某个类的指定方法_egMin() throws InvocationTargetException, IllegalAccessException, NoSuchFieldException, NoSuchMethodException {//e.g.
        EffectBo effectBo=new EffectBo();
        Class<? extends EffectBo> aClass = effectBo.getClass();
        //region 利用java 反映射 √
        //1.参数反射
        Field field = aClass.getDeclaredField("orgExpression");//必须完全相同 : orgExpression √;orgexpression X;
        field.setAccessible(true);
        field.set(effectBo, "参数反射");
        //2.获取反映射的class
        System.out.println("--------------------获取反映射的class--------------------");
        System.out.println("[名称]:" + field.getClass() + "\n"
                + "[特性效果]:" + field.getDeclaringClass() + "\n"
                + "[登场次数]:" + field.getDeclaredAnnotations() + "\n"
                + "[登场次数]:" + field.getType() + "\n"
        );
        //System.out.println(effectBo.getOrgExpression());

        //2.方法反射 √
        Method  add = aClass.getMethod("setOrgExpression",String.class);//参数要填
        //据说 getDeclaredMethod 能调用所有方法
        //getMethod :public
        //add.setAccessible(true);//可不填
        add.invoke(effectBo,"方法反射");
        effectBo.setOrgExpression("萨达");
        //System.out.println(effectBo.getOrgExpression());
        //endregion

        //region 利用hutool  setOrgExpression X 同上 要填写参数
        System.out.println("利用hutool");
        Method method = ReflectUtil.getMethod(aClass, "setOrgExpression",String.class);
        //ReflectUtil.invoke(effectBo.getClass(),method,"getOrgExpression");
        method.setAccessible(true);
        method.invoke(effectBo,"setOrgExpression");
        System.out.println(effectBo.getOrgExpression());
        //endregion
    }

    @Test
    public void 反映射_egMin() {//e.g.
        EffectBo effectBo=new EffectBo();
        effectBo.setOrgExpression("asd ");
        //Class<? extends EffectBo> aClass = effectBo.getClass();
        //2.获取参数 有值返回类型;无值为null
        Object orgExpression = ReflectUtil.getFieldValue(effectBo, "orgExpression");
        System.out.println(orgExpression.getClass());

        Object keywordsMap = ReflectUtil.getFieldValue(effectBo, "isSet");
        System.out.println(keywordsMap.getClass());

        //复杂对象也可以
        GameBo gameBo=new GameBo();
        Object gameDetailBoPlayTwo = ReflectUtil.getFieldValue(gameBo, "r");
        System.out.println(gameDetailBoPlayTwo.getClass());

        //1.反映射方法失败
        /*Object getOrgExpression = ReflectUtil.invoke(effectBo, "getOrgExpression");
        Object entityId = ReflectUtil.invoke(effectBo, "getOrgExpression");
        System.out.println(entityId.getClass());*/

        //2022年7月21日
        Object expandMapObject = ReflectUtil.getFieldValue(effectBo, "expandMap");
        HashMap expandMap = (HashMap)expandMapObject;
    }

    @Test
    public void egMin() {//e.g.
        System.out.println();
    }
}
